home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Library / Manuels & Misc / Assembly / AOA.ZIP / CH18 / RUN.ASM < prev    next >
Encoding:
Assembly Source File  |  1994-07-15  |  4.8 KB  |  188 lines

  1. ; RUN.ASM - The barebones semiresident program.
  2. ;
  3. ;    Usage:
  4. ;        RUN  <program.exe>  <program's command line>
  5. ;      or    RUN  <program.com>  <program's command line>
  6. ;
  7. ; RUN executes the specified program with the supplied command line parameters.
  8. ; At first, this may seem like a stupid program.  After all, why not just run
  9. ; the program directly from DOS and skip the RUN altogether?  Actually, there
  10. ; is a good reason for RUN-- It lets you (by modifying the RUN source file)
  11. ; set up some environment prior to running the program and clean up that
  12. ; environment after the program terminates ("environment" in this sense does
  13. ; not necessarily refer to the MS-DOS ENVIRONMENT area).
  14. ;
  15. ; For example, I have used this program to switch the mode of a TSR prior to
  16. ; executing an EXE file and then I restored the operating mode of that TSR
  17. ; after the program terminated.
  18. ;
  19. ; In general, you should create a new version of RUN.EXE (and, presumbably,
  20. ; give it a unique name) for each application you want to use this program
  21. ; with.
  22. ;
  23. ;
  24. ;----------------------------------------------------------------------------
  25. ;
  26. ;
  27. ; Put these segment definitions 1st because we want the Standard Library
  28. ; routines to load last in memory, so they wind up in the transient portion.
  29.  
  30. CSEG        segment    para public 'CODE'
  31. CSEG        ends
  32. SSEG        segment    para stack 'stack'
  33. SSEG        ends
  34. ZZZZZZSEG    segment    para public 'zzzzzzseg'
  35. ZZZZZZSEG    ends
  36.  
  37.  
  38. ; Includes for UCR Standard Library macros.
  39.  
  40.         include    consts.a
  41.         include stdin.a
  42.         include stdout.a
  43.         include misc.a
  44.         include memory.a
  45.         include    strings.a
  46.  
  47.         includelib stdlib.lib
  48.  
  49.  
  50. CSEG        segment    para public 'CODE'
  51.         assume    cs:cseg, ds:cseg
  52.  
  53.  
  54. ; Variables used by this program.
  55.  
  56.  
  57. ; MS-DOS EXEC structure.
  58.  
  59. ExecStruct    dw    0            ;Use parent's Environment blk.
  60.         dd    CmdLine            ;For the cmd ln parms.
  61.         dd    DfltFCB
  62.         dd    DfltFCB
  63.  
  64. DfltFCB        db    3,"           ",0,0,0,0,0
  65. CmdLine        db    0, 0dh, 126 dup (" ")    ;Cmd line for program.
  66. PgmName        dd    ?            ;Points at pgm name.
  67.  
  68.  
  69.  
  70.  
  71. Main        proc
  72.         mov    ax, cseg        ;Get ptr to vars segment
  73.         mov    ds, ax
  74.  
  75.         MemInit                ;Start the memory mgr.
  76.  
  77.  
  78.  
  79. ; If you want to do something before the execution of the command-line
  80. ; specified program, here is a good place to do it:
  81.  
  82.  
  83.  
  84. ;    -------------------------------------
  85.  
  86.  
  87.  
  88. ; Now let's fetch the program name, etc., from the command line and execute
  89. ; it.
  90.  
  91.         argc                ;See how many cmd ln parms
  92.         or    cx, cx            ; we have.
  93.         jz    Quit            ;Just quit if no parameters.
  94.  
  95.         mov    ax, 1            ;Get the first parm (pgm name)
  96.         argv
  97.         mov    word ptr PgmName, di    ;Save ptr to name
  98.         mov    word ptr PgmName+2, es
  99.  
  100.  
  101. ; Okay, for each word on the command line after the filename, copy
  102. ; that word to CmdLine buffer and separate each word with a space,
  103. ; just like COMMAND.COM does with command line parameters it processes.
  104.  
  105.         lea    si, CmdLine+1        ;Index into cmdline.
  106. ParmLoop:    dec    cx
  107.         jz    ExecutePgm
  108.  
  109.         inc    ax            ;Point at next parm.
  110.         argv                ;Get the next parm.
  111.  
  112.         push    ax
  113.         mov    byte ptr [si], ' '    ;1st item and separator on ln.
  114.         inc    CmdLine
  115.         inc    si
  116. CpyLp:        mov    al, es:[di]
  117.         cmp    al, 0
  118.         je    StrDone
  119.         inc    CmdLine            ;Increment byte cnt
  120.         mov    ds:[si], al
  121.         inc    si
  122.         inc    di
  123.         jmp    CpyLp
  124.  
  125. StrDone:    mov    byte ptr ds:[si], cr    ;In case this is the end.
  126.         pop    ax            ;Get current parm #
  127.         jmp    ParmLoop
  128.  
  129.  
  130. ; Okay, we've built the MS-DOS execute structure and the necessary
  131. ; command line, now let's see about running the program.
  132. ; The first step is to free up all the memory that this program
  133. ; isn't using.  That would be everything from zzzzzzseg on.
  134.  
  135. ExecutePgm:     mov    ah, 62h            ;Get our PSP value
  136.         int    21h
  137.         mov    es, bx
  138.         mov    ax, zzzzzzseg        ;Compute size of
  139.         sub    ax, bx            ; resident run code.
  140.         mov    bx, ax
  141.         mov    ah, 4ah            ;Release unused memory.
  142.         int    21h
  143.  
  144. ; Warning!  No Standard Library calls after this point.  We've just
  145. ; released the memory that they're sitting in.  So the program load
  146. ; we're about to do will wipe out the Standard Library code.
  147.  
  148.         mov    bx, seg ExecStruct
  149.         mov    es, bx
  150.         mov    bx, offset ExecStruct    ;Ptr to program record.
  151.         lds    dx, PgmName
  152.         mov    ax, 4b00h        ;Exec pgm
  153.         int    21h
  154.  
  155. ; When we get back, we can't count on *anything* being correct.  First, fix
  156. ; the stack pointer and then we can finish up anything else that needs to
  157. ; be done.
  158.  
  159.         mov    ax, sseg
  160.         mov    ss, ax
  161.         mov    sp, offset EndStk
  162.         mov    ax, seg cseg
  163.         mov    ds, ax
  164.  
  165. ; Okay, if you have any great deeds to do after the program, this is a
  166. ; good place to put such stuff.
  167.  
  168. ;    -------------------------------------
  169.  
  170. ; Return control to MS-DOS
  171.  
  172. Quit:        ExitPgm
  173. Main        endp
  174. cseg        ends
  175.  
  176. sseg        segment    para stack 'stack'
  177.         dw    128 dup (0)
  178. endstk        dw    ?
  179. sseg        ends
  180.  
  181. ; Set aside some room for the heap.
  182.  
  183. zzzzzzseg    segment    para public 'zzzzzzseg'
  184. Heap        db    200h dup (?)
  185. zzzzzzseg    ends
  186.  
  187.         end    Main
  188.